<?php

namespace App\Services;

use App\Document;
use Illuminate\Support\Facades\Cache;

class SettingService extends BaseService
{
    //专利申请前评估评分指标
    public static function patentRetrievalReportTemplateATag($title = '下载'): string
    {
        return self::templateATag('patent_retrieval_template', [
            url('template/retrieval_report_template.docx'),
            '查新检索报告模板.docx',
        ], $title);
    }

    //专利申请前评估评分指标
    public static function patentPfTemplateATag($title = '下载'): string
    {
        return self::templateATag('patent_pf_template', [
            url('template/score-template.docx'),
            '专利申请前评估评分指标.docx',
        ], $title);
    }

    //计算机软件著作权登记备案表(模板)
    public static function bookFilingTemplateATag($title = '模板'): string
    {
        return self::templateATag('book_filing_template', [
            url('/template/book_apply_template.docx'),
            '计算机软件著作权登记备案表.docx',
        ], $title);
    }

    //科研项目伦理与安全风险自查及防范承诺书
    public static function transversePromiseTemplateATag($title = ''): string
    {
        return self::templateATag('transverse_promise_template', [], $title);
//        return self::templateATag('transverse_promise_template', [
//            url('/template/transverse_promise_template.docx'),
//            '科研项目伦理与安全风险自查及防范承诺书.docx',
//        ], $title);
    }

    protected static function templateATag($slug, $default = [], $title = ''): string
    {
        $model = Document::allSettingCollection()->where('slug', $slug)->first();
        if ($model instanceof Document) {
            return sprintf('<a target="_blank" href="%s" download="%s">%s</a>&nbsp;&nbsp;',
                file_preview_url($model->path, $model->name),
                $model->name,
                $title ?: $model->name
            );
        }
        return $default ? sprintf('<a target="_blank" href="%s" download="%s">%s</a>&nbsp;&nbsp;',
            $default[0],
            $default[1],
            $title ?: data_get($default, 2, $default[1])
        ) : '';
    }

}
